<?php
/*
 * ebay API GetSellerList
* 获取卖家的 Listing
*
*/
class Helper_BSS_API_EBAY_GetSellerList
{
	
	static function GetSellerList($_Data)
	{
		//$_Data = array('starttime'=>$starttime,'endtime'=>$endtime,'shop_id'=>$shop_id);
	
		$shop_id = $_Data['shop_id'];
		$APICallName = 'GetSellerList';
		$starttime = date('c', strtotime($_Data['starttime']));
		$endtime = date('c', strtotime(empty($_Data['endtime']) ? CURRENT_DATETIME : ($_Data['endtime'] . ' 23:59:59')));
		$timedatatype = $_Data['timedatatype'];
		
		if($timedatatype == 'up')
		{
			$condTime ='  <StartTimeFrom>' . $starttime . '</StartTimeFrom> ';
			$condTime .='  <StartTimeTo>' . $endtime . '</StartTimeTo> ';
		}else {
			$condTime ='  <EndTimeFrom>' . $starttime . '</EndTimeFrom> ';
			$condTime .='  <EndTimeTo>' . $endtime . '</EndTimeTo> ';
		}
	
		set_time_limit(1500);
		$token = Ebay_Token::find('shop_id=?', $shop_id)->asArray()->getOne();
	
		if ($token)
		{
			$perpage = 100;
			$pagepumber = 0;
			$total_entries = 1;
			$header = array(
					'X-EBAY-API-COMPATIBILITY-LEVEL:' . Q::ini('custom_api/X-EBAY-API-COMPATIBILITY-LEVEL'),
					'X-EBAY-API-DEV-NAME:' . Q::ini('custom_api/X-EBAY-API-DEV-NAME'),
					'X-EBAY-API-APP-NAME:' . Q::ini('custom_api/X-EBAY-API-APP-NAME'),
					'X-EBAY-API-CERT-NAME:' . Q::ini('custom_api/X-EBAY-API-CERT-NAME'),
					'X-EBAY-API-SITEID:0',
					'X-EBAY-API-CALL-NAME:' . $APICallName
			);
	
			for($i=0; $i < $total_entries; $i=$perpage*$pagepumber)
			{
	
				$pagepumber++;
	
				$requestXmlBody ='<?xml version="1.0" encoding="utf-8"?>';
				$requestXmlBody .='<GetSellerListRequest xmlns="urn:ebay:apis:eBLBaseComponents">';
				$requestXmlBody .='  <RequesterCredentials>';
				$requestXmlBody .='    <eBayAuthToken>' . $token['token'] . '</eBayAuthToken>';
				$requestXmlBody .='  </RequesterCredentials>';
				$requestXmlBody .='  <ErrorLanguage>en_US</ErrorLanguage>';
				//$requestXmlBody .='  <WarningLevel>High</WarningLevel>';
				//$requestXmlBody .='  <GranularityLevel>Fine</GranularityLevel>';
				$requestXmlBody .='  <DetailLevel>ReturnAll</DetailLevel>';
				
				$requestXmlBody .= $condTime ;
	
				$requestXmlBody .='  <IncludeVariations>true</IncludeVariations>';
				//$requestXmlBody .='  <IncludeWatchCount>true</IncludeWatchCount> ';
				$requestXmlBody .='  <Pagination> ';
				$requestXmlBody .='    <EntriesPerPage>' . $perpage . '</EntriesPerPage>';
				$requestXmlBody .='    <PageNumber>' . $pagepumber . '</PageNumber>';
				$requestXmlBody .='  </Pagination>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.ItemID</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.Currency</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.StartPrice</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.Title</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.SKU</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.ListingDetails.StartTime</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.ListingDetails.EndTime</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.ListingDetails.viewitemurl</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.PictureDetails.galleryurl</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.ListingType</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.Quantity</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.SellingStatus.QuantitySold</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.PrimaryCategory.CategoryID</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.PrimaryCategory.CategoryName</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.Variations.Variation.Quantity</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.Variations.Variation.StartPrice</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.Variations.Variation.SellingStatus.QuantitySold</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ItemArray.Item.Variations.Variation.SKU</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>PaginationResult</OutputSelector>';
				$requestXmlBody .=' <OutputSelector>ReturnedItemCountActual</OutputSelector>';
	
				$requestXmlBody .='</GetSellerListRequest>';
	
				$response_xml = Helper_BSS_API::ebayAPICall(Q::ini('custom_api/ebay_ws_uri'), $header, $requestXmlBody, $pagepumber);
				$sellerlist_xml = simplexml_load_string($response_xml);
				unset($response_xml);
	
				$total_entries = (int)$sellerlist_xml -> PaginationResult[0] -> TotalNumberOfEntries[0];
	
				if ((int)$sellerlist_xml -> ReturnedItemCountActual)
				{
					$XmlData = self::XmlToArray($sellerlist_xml, $token);
					unset($sellerlist_xml);
					Online_Ebay_Listing::import($XmlData);
				}
	
			} //end for
	
		} //end if token
	
		//return $this->_redirect(url('Online::Ebay/PageCheckNumberOnlineInfo',array('starttime'=>$_POST['starttime'],'endtime'=>$_POST['endtime'],'shop_id'=>$shop_id)));
	
	}
	
	/*
	 * ebay API GetSellerList 的返回数据转化 xmltoarray
	*/
	static function XmlToArray($object_xml, $token)
	{
		$XmlData = $ListingData = $NewListingData = $VariationData = $NewVariationData = array();
		$j = 0;
	
		$ResultNums = (int)$object_xml -> ReturnedItemCountActual[0];
	
		if ($ResultNums > 0)
		{
	
			foreach($object_xml->ItemArray[0] as $key=>$i)
			{
	
				$ListingData['shop_id'] = $token['shop_id'];
				$ListingData['itemid'] = (string)$i->ItemID[0];
	
				$ListingData['title'] = (string)$i->Title[0];
	
				$ListingData['currency'] = (string)$i->Currency[0];
	
				$ListingData['listingtype'] = (string)$i->ListingType[0];
				$ListingData['startprice'] = (string)$i->StartPrice[0] ;
				$ListingData['quantitysold'] = (string)$i->SellingStatus[0]->QuantitySold[0];
				$ListingData['categoryid'] = (string)$i->PrimaryCategory[0]->CategoryID[0];
				$ListingData['categoryname'] = (string)$i->PrimaryCategory[0]->CategoryName[0];
				$ListingData['starttime'] = date('Y-m-d H:i:s', strtotime((string)$i->ListingDetails[0]->StartTime[0]));
				$ListingData['endtime'] = date('Y-m-d H:i:s', strtotime((string)$i->ListingDetails[0]->EndTime[0]));
				$ListingData['viewitemurl'] = (string)$i->ListingDetails[0]->ViewItemURL[0];
				$ListingData['galleryurl'] = (string)$i->PictureDetails[0]->GalleryURL[0];
				$ListingData['quantity'] = (string)$i->Quantity[0];
	
				if(!empty($i->SKU[0]))
				{
					$ListingData['number'] = (string)$i->SKU[0];
	
					$VariationData[0]['itemid'] = $ListingData['itemid'];
					$VariationData[0]['shop_id'] = $ListingData['shop_id'];
					$VariationData[0]['number'] = $ListingData['number'];
					$VariationData[0]['startprice'] = $ListingData['startprice'];
					$VariationData[0]['quantitysold'] = $ListingData['quantitysold'];
					$VariationData[0]['quantity'] = (int)$ListingData['quantity'];
					$VariationData[0]['note'] = $ListingData['number'];
	
				}else{
	
					$ListingData['number'] = '000000';
	
					if(!empty($i->Variations[0]))
					{
	
						foreach ($i->Variations[0] as $iv)
						{
							$currentSku = (string)$iv->SKU[0];
							$note = $currentSku;
	
							$currentSku = strlen($currentSku) > 10 ? '000000' : $currentSku ;
							$VariationData[$j]['shop_id'] = $ListingData['shop_id'];
							$VariationData[$j]['itemid'] = $ListingData['itemid'];
							$VariationData[$j]['number'] = $currentSku;
							$VariationData[$j]['note'] = $note;
							$VariationData[$j]['startprice'] = (string)$iv->StartPrice[0];
							$VariationData[$j]['quantitysold'] = (string)$iv->SellingStatus[0]->QuantitySold[0];
							$VariationData[$j]['quantity'] = (int)$iv->Quantity[0];
	
							$j++;
						}
	
					}else{
						$VariationData = array();
					}
	
				}
	
				$NewListingData[] = $ListingData;
				$NewVariationData[] = $VariationData;
	
			}
	
	
		}
	
		//Merger
		$XmlData['Listing'] = $NewListingData;
		$XmlData['Variation'] = $NewVariationData;
	
		return $XmlData;
	
	}
}